package com.zmn.mcc.admin.controller.test;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.cache.StaffCache;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.common.dto.staff.StaffSimpleQueryDIO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.mcc.model.entity.staff.McStaff;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Param;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/07/02 18:59
 */
@Controller
@RequestMapping("/test/staff")
public class StaffTestController {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private StaffListRemoteService staffListRemoteService;

    @Resource
    private StaffCache staffCache;


    /**
     * 远程调用-取得员工列表
     */
    @RequestMapping(value = "dubbo/list")
    @ResponseBody
    public ResponseDTO dubboList(HttpServletRequest request, Model model) {

        ResponseDTO<List<StaffDRO>> response = staffListRemoteService.listStaffByKeyword("1370", GlobalDict.STATUS_ENABLE);
        return response;
    }

    /**
     * 远程调用-取得员工kv对象列表
     */
    @RequestMapping(value = "dubbo/listKv")
    @ResponseBody
    public ResponseDTO dubboListKv(HttpServletRequest request, Model model) {

        StaffSimpleQueryDIO query = new StaffSimpleQueryDIO();
        query.setKeyword("李奇");
        query.setStatus(GlobalDict.STATUS_ENABLE);

        ResponseDTO<List<VtDTO>> response = staffListRemoteService.listStaffVtBySimpleQueryDIO(query);
        return response;
    }


    /**
     * 远程调用-取得员工kv对象列表
     */
    @RequestMapping(value = "dubbo/testRoleIds")
    @ResponseBody
    public ResponseDTO listStaffByRoleIdsIncludeRoleId(HttpServletRequest request, Model model) {
        return staffListRemoteService.listStaffByRoleIdsIncludeRoleId(Arrays.asList(283,285,377),GlobalConsts.YES);
    }


    /**
     * 远程调用-取得员工列表
     */
    @RequestMapping(value = "dubbo/listIncludeSubDept")
    @ResponseBody
    public ResponseDTO listIncludeSubDept(HttpServletRequest request, Model model) {
        StaffSimpleQueryDIO dio = new StaffSimpleQueryDIO();
        dio.setDeptIds(Arrays.asList(4333));
        dio.setKeyword("郝传全");
        dio.setPlat(0);
        ResponseDTO<List<StaffDRO>> response = staffListRemoteService.listSubAllDeptStaffBySimpleQueryDIO(dio);
        return response;
    }

    /**
     * 从缓存中获取员工数据
     */
    @RequestMapping(value = "cache/get")
    @ResponseBody
    public ResponseDTO get(@Param("staffId") Integer staffId) {
        McStaff mcStaff = staffCache.getStaff(staffId);
        BeanCopier copier = BeanCopier.create(McStaff.class, StaffDRO.class, false);
        StaffDRO dro = new StaffDRO();
        copier.copy(mcStaff, dro, null);
        return ResponseDTO.success(dro);
    }
}
